# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: baidu.py
@time: 2021/8/24 13:51
'''
from pprint import pprint

'''
from: zh
to: en
query: 菲菲
transtype: realtime
simple_means_flag: 3
sign: 813898.559227
token: fe3327b2117eb057f57d5bbf055c38f8
domain: common


print(execjs.eval('new Date'))         #北京时间
print(execjs.eval('Date.now()'))       #时间戳 毫秒为单位

print(execjs.get().name)               #获得js环境
js ="""
function add(x,y){
    return x+y
}
"""
etx = execjs.compile(js)
print(etx.call('add',1,2))

'''

import requests

import requests
import execjs

def feifei(query):

    js = open(r'D:\TulingPaCong\day15js逆向百度翻译\baidu_transfer.js','r').read()
    ctx = execjs.compile(js)
    sign = ctx.call('e',query)

    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36',
        'referer': 'https://fanyi.baidu.com/',
        'cookie': 'BIDUPSID=9EB4B080CEDC1652908BC2718A97DEE9; PSTM=1629429970; BAIDUID=9EB4B080CEDC16529356AE97AEC640FD:FG=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; REALTIME_TRANS_SWITCH=1; HISTORY_SWITCH=1; SOUND_PREFER_SWITCH=1; SOUND_SPD_SWITCH=1; FANYI_WORD_SWITCH=1; BAIDUID_BFESS=880BD9491621ABD1729CEB0769CA90BC:FG=1; delPer=0; PSINO=7; H_PS_PSSID=34435_34004_34073_34092_34106_26350_34319_34390; BA_HECTOR=0g8h00ala42k2g0knn1gi92090r; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1629783665,1629783712,1629783719,1629784080; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1629785057; __yjs_st=2_NDE4ZjY5ODA4ODU1MDA2N2NmYTZlNDY3YjQ3NzRiNmJkNDBmOGFlNmZkMDI2YmQ4MGE5ZWYwY2JiNmE4ZmMxMzkyNDNmOWZlY2M3YTA0MzJlYWVhMGQwZTRiY2M5NmI0OThhMGJiMjhkNjAwNGNhNWQ0NjRkN2ZjYmVmNTY5ZTQxZDQyYTFiNmRlMjhkOTBhNzM3M2MxZWI0NGVjYTFmN2ZiM2Q4YTI5ZTBiYTc3YzU3ZmNjOTAzMmE3YzAwNmRlNzgxOGNlNzQ0ZDBkNGM3OWYxMjYxMmQ1MzQyM2VjZmU2N2M3Y2VjYjIzMDYzN2U5MTI2M2ZkM2RiYmMwODU4NF83X2IxZWFlMjZl',
    }
    data = {
      'query': query,
      'from': 'zh',
      'to': 'en',
      'simple_means_flag': '3',
      'sign': sign,
              #813898.559227
              #813898.559227  菲菲  没有时间戳(查询两次菲菲,sign的值都一样)
              #395687.191126  菲宝
              #395687.191126
      'token': 'fe3327b2117eb057f57d5bbf055c38f8',
               #fe3327b2117eb057f57d5bbf055c38f8  菲菲
               #fe3327b2117eb057f57d5bbf055c38f8  菲宝
      'domain': 'common',
      'transtype': 'translang',
    }
    res = requests.post('https://fanyi.baidu.com/v2transapi?from=zh&to=en', headers=headers,data=data)

    dic = res.json()
    # print(dic)
    text = dic.get('trans_result')['data'][0]['dst']
    print(f'"{query}"的英文意思是:{text}')

if __name__ == '__main__':
    while True:
        str = input('请输入:')
        if str == 'exit':
            break
        feifei(str.strip())

 #   小红书

    # js = '''
    # function pt() {
    #         for (var t = "", e = 0; e < 16; e++)
    #             t += "abcdef0123456789".charAt(Math.floor(16 * Math.random()));
    #         return t
    #     }
    # '''
    #
    # ctx = execjs.compile(js)
    # ttt = ctx.call('pt')
    # print(ttt)








